<?php
/**
 * Created by PhpStorm.
 * User: lee
 * Date: 2018/1/9
 * Time: 下午3:39
 */

namespace Biz\model\trade;

use Biz\pockets\constInterface\PayConfig;

class OrderPay extends \Doraemon\pockets\datebase\BaseDbModel implements PayConfig
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_trade';

    const _DATABASE_ = 'shop_trade';

    const _TABLE_NAME = 'order_pay';

    const STATUS_NOTPAY = 1;
    const STATUS_PAIED = 2;
    const STATUS_CLOSED = 0;
    const STATUS_CANCEL = -1;

    const PAY_STATUS_DESC = [
        self::STATUS_NOTPAY => '未支付',
        self::STATUS_PAIED => '已支付',
        self::STATUS_CLOSED => '已关闭',
        self::STATUS_CANCEL => '已自动退款'
    ];

    public static function addCreateOrderInfo($payWay, $orderNo, $payAmount, $payOrderInfo, $ordreType = self::_PAY_ORDER_TYPE_BIZ, $payVer = 0)
    {
        $lastPayInfo = self::get("*", [
            'order_no' => $orderNo,
            'pay_way' => $payWay,
            'order_type' => $ordreType,
            'pay_ver' => $payVer
        ]);
        if ($lastPayInfo) {
            self::update([
                'pay_order_info' => json_encode($payOrderInfo),
                'create_order_time' => date('Y-m-d H:i:s')
            ], [
                'order_no' => $orderNo,
                'pay_way' => $payWay,
                'order_type' => $ordreType
            ]);
        } else {
            self::insert([
                'order_no' => $orderNo,
                'pay_way' => $payWay,
                'pay_amount' => $payAmount,
                'pay_order_info' => json_encode($payOrderInfo),
                'create_order_time' => date('Y-m-d H:i:s'),
                'order_type' => $ordreType,
                'pay_ver' => $payVer
            ]);
        }
    }

    public static function addPayOrderInfo($payWay, $orderNo, $payVer, $payNo, $amount, $payTime, $callBackInfo)
    {
        self::update([
            'callback_info' => json_encode($callBackInfo),
            'pay_no' => $payNo,
            'pay_amount' => $amount,
            'pay_time' => $payTime,
            'status' => self::STATUS_PAIED
        ], [
            'order_no' => $orderNo,
            'pay_ver' => $payVer,
            'pay_way' => $payWay,
            'status' => self::STATUS_NOTPAY
        ]);
        return self::get("*", [
            'order_no' => $orderNo,
            'pay_ver' => $payVer,
            'pay_way' => $payWay
        ]);
    }

    public static function setPayOrderStatus($payWay, $orderNo, $payVer, $status)
    {
        self::update([
            'status' => $status,
            'edit_time' => date('Y-m-d H:i:s')
        ], [
            'order_no' => $orderNo,
            'pay_way' => $payWay,
            'pay_ver' => $payVer
        ]);
    }

    public static function getPayInfoByOrderNo($orderNo, $orderType = self::_PAY_ORDER_TYPE_BIZ)
    {
        return self::select("*",
            [
                'order_no' => $orderNo,
                'order_type' => $orderType,
                "ORDER" => [
                    'pay_way' => self::_PAY_WAY_ORDER
                ]
            ]);
    }

    public static function getPayInfoByOrderNoAndPayWay($orderNo, $payWay)
    {
        return self::get("*", [
            'order_no' => $orderNo,
            'pay_way' => $payWay,
        ]);
    }

    public static function getPayInfo($payNo, $payWay = null)
    {
        $where = ['pay_no' => $payNo];
        if ($payWay) {
            $where['pay_way'] = $payWay;
        }
        return self::get("*", $where);
    }

    public static function getPayInfos($payNo)
    {
        return self::select("*", [
            'pay_no' => $payNo,
            'status[!]' => self::STATUS_CLOSED,
            'ORDER' => ['status' => 'DESC']
        ]);
    }

    public static function getOrderPayList($field, $orderPayWhere, $order, $limit = [])
    {

        if (isset($orderPayWhere['payWay']) && $orderPayWhere['payWay']) {
            $where['AND']['pay_way'] = $orderPayWhere['payWay'];
        }
        if (isset($orderPayWhere['start_time']) && $orderPayWhere['start_time']) {
            $where['AND']['pay_time[>]'] = $orderPayWhere['start_time'];
        }
        if (isset($orderPayWhere['end_time']) && $orderPayWhere['end_time']) {
            $where['AND']['pay_time[<=]'] = $orderPayWhere['end_time'];
        }

        if (isset($orderPayWhere['orderNo']) && $orderPayWhere['orderNo']) {
            $where['AND']['order_no'] = [
                $orderPayWhere['orderNo'],
                $orderPayWhere['orderPno']
            ];
        }
        if (isset($orderPayWhere['payNo']) && $orderPayWhere['payNo']) {
            $where['AND']['pay_no'] = $orderPayWhere['payNo'];
        }
        if ($limit) {
            $where['LIMIT'] = $limit;
        }

        if (isset($orderPayWhere['status'])) {

            $where['AND']['status'] = $orderPayWhere['status'];
        }

        $where['ORDER'] = ['pay_time' => 'DESC'];

        return self::select($field, $where);
    }

    public static function getOrderPayCount($orderPayWhere)
    {

        if (isset($orderPayWhere['payWay']) && $orderPayWhere['payWay']) {
            $where['AND']['pay_way'] = $orderPayWhere['payWay'];
        }
        if (isset($orderPayWhere['start_time']) && $orderPayWhere['start_time']) {
            $where['AND']['pay_time[>]'] = $orderPayWhere['start_time'];
        }
        if (isset($orderPayWhere['end_time']) && $orderPayWhere['end_time']) {
            $where['AND']['pay_time[<=]'] = $orderPayWhere['end_time'];
        }
        if (isset($orderPayWhere['orderNo']) && $orderPayWhere['orderNo']) {
            $where['AND']['order_no'] = [
                $orderPayWhere['orderNo'],
                $orderPayWhere['orderPno']
            ];
        }
        if (isset($orderPayWhere['payNo']) && $orderPayWhere['payNo']) {
            $where['AND']['pay_no'] = $orderPayWhere['payNo'];
        }
        if (isset($orderPayWhere['status'])) {

            $where['AND']['status'] = $orderPayWhere['status'];
        }

        return self::count($where);
    }

    public static function getOrderPayAmount($orderPayWhere)
    {

        if (isset($orderPayWhere['payWay']) && $orderPayWhere['payWay']) {
            $where['AND']['pay_way'] = $orderPayWhere['payWay'];
        }
        if (isset($orderPayWhere['start_time']) && $orderPayWhere['start_time']) {
            $where['AND']['pay_time[>]'] = $orderPayWhere['start_time'];
        }
        if (isset($orderPayWhere['end_time']) && $orderPayWhere['end_time']) {
            $where['AND']['pay_time[<=]'] = $orderPayWhere['end_time'];
        }
        if (isset($orderPayWhere['orderNo']) && $orderPayWhere['orderNo']) {
            $where['AND']['order_no'] = [
                $orderPayWhere['orderNo'],
                $orderPayWhere['orderPno']
            ];
        }
        if (isset($orderPayWhere['payNo']) && $orderPayWhere['payNo']) {
            $where['AND']['pay_no'] = $orderPayWhere['payNo'];
        }
        if (isset($orderPayWhere['status'])) {

            $where['AND']['status'] = $orderPayWhere['status'];
        }
        return self::sum('pay_amount', $where);
    }

    // 暂时使用方法 导出可能改为单页导出和导出全部
    public static function getOrderPay($field, $orderPayWhere)
    {

        if (isset($orderPayWhere['payWay']) && $orderPayWhere['payWay']) {
            $where['AND']['pay_way'] = $orderPayWhere['payWay'];
        }
        if (isset($orderPayWhere['start_time']) && $orderPayWhere['start_time']) {
            $where['AND']['pay_time[>]'] = $orderPayWhere['start_time'];
        }
        if (isset($orderPayWhere['end_time']) && $orderPayWhere['end_time']) {
            $where['AND']['pay_time[<=]'] = $orderPayWhere['end_time'];
        }
        if (isset($orderPayWhere['status']) && $orderPayWhere['status']) {
            $where['AND']['status'] = $orderPayWhere['status'];
        }
        if (isset($orderPayWhere['orderNo']) && $orderPayWhere['orderNo']) {
            $where['AND']['order_no'] = [
                $orderPayWhere['orderNo'],
                $orderPayWhere['orderPno']
            ];
        }
        if (isset($orderPayWhere['payNo']) && $orderPayWhere['payNo']) {
            $where['AND']['pay_no'] = $orderPayWhere['payNo'];
        }

        return self::select($field, $where);
    }

}